package com.hckjsec.website.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import io.netty.util.internal.StringUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteNews;
import com.hckjsec.website.service.IWebSiteNewsService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 新闻动态Controller
 * 
 * @author cyt
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/webnews")
public class WebSiteNewsController extends BaseController
{
    @Autowired
    private IWebSiteNewsService webSiteNewsService;

    /**
     * 查询新闻动态列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteNews webSiteNews)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteNews> list = webSiteNewsService.selectWebSiteNewsList(webSiteNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出新闻动态列表
     */
    @Log(title = "新闻动态", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteNews webSiteNews)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteNews> list = webSiteNewsService.selectWebSiteNewsList(webSiteNews);
        ExcelUtil<WebSiteNews> util = new ExcelUtil<WebSiteNews>(WebSiteNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取新闻动态详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webSiteNewsService.selectWebSiteNewsById(id));
    }

    /**
     * 新增新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteNews webSiteNews)
    {
        if(StringUtils.isNull(webSiteNews.getPublishTime())){
            webSiteNews.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteNews.getContent(),""))){//解密
            webSiteNews.setContent(Base64Converter.decode(webSiteNews.getContent()));
        }else{
            webSiteNews.setContent(webSiteNews.getContent());
        }
        webSiteNews.setDeptId(SecurityUtils.getDeptId());
        webSiteNews.setOperator(SecurityUtils.getUserId());
        return toAjax(webSiteNewsService.insertWebSiteNews(webSiteNews));
    }

    /**
     * 修改新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteNews webSiteNews)
    {
        if(StringUtils.isNull(webSiteNews.getPublishTime())){
            webSiteNews.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteNews.getContent(),""))){//解密
            webSiteNews.setContent(Base64Converter.decode(webSiteNews.getContent()));
        }else{
            webSiteNews.setContent(webSiteNews.getContent());
        }
        return toAjax(webSiteNewsService.updateWebSiteNews(webSiteNews));
    }

    /**
     * 删除新闻动态
     */
    @Log(title = "新闻动态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteNewsService.deleteWebSiteNewsByIds(ids));
    }
}
